package com.yxy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yxy.common.PageResult;
import com.yxy.context.BaseContext;
import com.yxy.mapper.CollectMapper;
import com.yxy.mapper.HotelMapper;
import com.yxy.mapper.SightsMapper;
import com.yxy.pojo.dto.HotelCollectPageQueryDTO;
import com.yxy.pojo.dto.SightsCollectPageQueryDTO;
import com.yxy.pojo.entity.Hotel;
import com.yxy.pojo.entity.HotelCollect;
import com.yxy.pojo.entity.Sights;
import com.yxy.pojo.entity.SightsCollect;
import com.yxy.pojo.vo.HotelCollectPageQueryVO;
import com.yxy.pojo.vo.SightsCollectPageQueryVO;
import com.yxy.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CollectServiceImpl implements CollectService {

    @Autowired
    private CollectMapper collectMapper;
    @Autowired
    private SightsMapper sightsMapper;
    @Autowired
    private HotelMapper hotelMapper;

    /**
     * 修改景点收藏状态
     * @param id
     * @param status
     */
    @Override
    public void updateSightsStatus(Integer id, Integer status) {
        SightsCollect sightsCollect = SightsCollect.builder()
                .userId(BaseContext.getCurrentId())
                .sightsId(id)
                .status(status)
                .build();
        collectMapper.updateSightsStatus(sightsCollect);
    }

    /**
     * 修改酒店收藏状态
     * @param id
     * @param status
     */
    @Override
    public void uppdateHotelStatus(Integer id, Integer status) {
        HotelCollect hotelCollect = HotelCollect.builder()
                .userId(BaseContext.getCurrentId())
                .hotelId(id)
                .status(status)
                .build();
        collectMapper.updateHotelStatus(hotelCollect);
    }

    /**
     * 查询景点收藏
     * @param sightsCollectPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuerySightsCollect(SightsCollectPageQueryDTO sightsCollectPageQueryDTO) {
        PageHelper.startPage(sightsCollectPageQueryDTO.getPage(), sightsCollectPageQueryDTO.getPageSize());
        Page<SightsCollectPageQueryVO> page = collectMapper.pageQuerySightsCollect(sightsCollectPageQueryDTO);
        page.getResult().forEach(item -> {
            // 根据景点id查询信息
            Sights sights = sightsMapper.selectById(item.getSightsId());
            item.setName(sights.getName());
            item.setCover(sights.getCover());
        });
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 查询酒店收藏
     * @param hotelCollectPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQueryHotelCollect(HotelCollectPageQueryDTO hotelCollectPageQueryDTO) {
        PageHelper.startPage(hotelCollectPageQueryDTO.getPage(), hotelCollectPageQueryDTO.getPageSize());
        Page<HotelCollectPageQueryVO> page = collectMapper.pageQueryHotelCollect(hotelCollectPageQueryDTO);
        page.getResult().forEach(item -> {
            // 根据酒店id查询信息
            Hotel hotel = hotelMapper.selectById(item.getHotelId());
            item.setName(hotel.getName());
            item.setCover(hotel.getCover());
        });
        return new PageResult(page.getTotal(), page.getResult());
    }
}
